package com.example.health_life.service;


import com.example.health_life.dao.Article;
import com.example.health_life.dao.ArticleRepository;
import com.example.health_life.dto.UsersDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ArticleServiceImpl implements ArticleService{

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private UserService userService;

    @Override
    public List<Article> getArticleList() {
        return articleRepository.findAll();
    }

    @Override
    public List<Article> getArticleListByType(Byte type) {
        return articleRepository.findByType(type);
    }

    @Override
    public Article getArticleById(Integer id) {
        return articleRepository.findById(id).get();
    }

    @Override
    public int addArticle(Article article) {
        // 设置默认值
        if (article.getFavorite() == null) {
            article.setFavorite(0);
        }
        if (article.getView() == null) {
            article.setView(0);
        }
        //将创建时间设置为当前时间
        article.setPublishTime(java.time.Instant.now());
        article.setStatus((byte) 1);
        return articleRepository.save(article).getId();
    }


    @Override
    public int deleteArticle(Integer id) {
        //判断文章是否存在
        articleRepository.findById(id).orElseThrow(()->new IllegalStateException("文章不存在"));

        // 存在，则将其status设置相反数
        int status  = articleRepository.findById(id).get().getStatus();//
        if (status == 1) {
            status = -1;
        } else {
            status = 1;
        }
        Optional<Article> article = articleRepository.findById(id);
        article.get().setStatus((byte) status);
        articleRepository.save(article.get());//保存改变的文章
        return id;
    }

    @Override
    public int updateArticle(Integer id, String title, String content, Integer  type, String coverImg,
                             Integer view, Integer  favorite, Integer status) {
        //判断文章是否存在
        articleRepository.findById(id).orElseThrow(()->new IllegalStateException("文章不存在"));

        Optional<Article> article1 = articleRepository.findById(id);

        if(type == 1 || type == 2 || type == 0){
            int type1 = type;
            article1.get().setType((byte) type1);
        }

        if(status ==  1){
            article1.get().setStatus((byte) 1);
        }
        if (status == -1){
            article1.get().setStatus((byte) -1);
        }

        if(view != -9){
            article1.get().setView(view);
        }
        article1.get().setTitle(title);
        article1.get().setContent(content);
        article1.get().setCoverImg(coverImg);

        if(favorite != -9){
            article1.get().setFavorite(favorite);
        }

        articleRepository.save(article1.get());
        return id;
    }

    @Override
    public List<Article> getArticleListWithAuthor() {
        List<Article> articles = articleRepository.findAll();
        for (Article article : articles) {
            UsersDTO author = userService.getUserById(article.getAuthorId());
            if (author != null) {
                article.setAuthorNickname(author.getNickname());
            }
        }
        return articles;
    }

    @Override
    public List<Article> getArticleListWithAuthor(Integer status) {
        List<Article> articles;
        if (status == 0) {
            articles = articleRepository.findAll();
        } else {
            articles = articleRepository.findByStatus((byte) status.intValue());
        }
        for (Article article : articles) {
            UsersDTO author = userService.getUserById(article.getAuthorId());
            if (author != null) {
                article.setAuthorNickname(author.getNickname());
            }
        }
        return articles;
    }

    @Override
    public Article getArticleByIdWithAuthor(Integer id) {
        Article article = articleRepository.findById(id)
                .orElseThrow(() -> new IllegalStateException("文章不存在"));
        UsersDTO author = userService.getUserById(article.getAuthorId());
        if (author != null) {
            article.setAuthorNickname(author.getNickname());
        }
        return article;
    }

    @Override
    public List<Article> getArticleListByTypeWithAuthor(Byte type) {
        List<Article> articles = articleRepository.findByType(type);
        for (Article article : articles) {
            UsersDTO author = userService.getUserById(article.getAuthorId());
            if (author != null) {
                article.setAuthorNickname(author.getNickname());
            }
        }
        return articles;
    }

    @Override
    public List<Article> getArticleListByTypeWithAuthor(Byte type, Integer status) {
        List<Article> articles;
        if (status == 0) {
            articles = articleRepository.findByType(type);
        } else {
            articles = articleRepository.findByTypeAndStatus(type, (byte) status.intValue());
        }
        for (Article article : articles) {
            UsersDTO author = userService.getUserById(article.getAuthorId());
            if (author != null) {
                article.setAuthorNickname(author.getNickname());
            }
        }
        return articles;
    }
}
